package servlets;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import domain.Contact;
import domain.IDAOContact;

public class FillDatabaseAction extends Action {
	public ActionForward execute(final ActionMapping pMapping,
			ActionForm pForm, final HttpServletRequest request,
			final HttpServletResponse response) {
		if (request.getMethod().equals("GET")) {
			ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServlet().getServletContext());
			IDAOContact daoContact = (IDAOContact) context.getBean("daocontact");
			Contact contact1 = (Contact) context.getBean("contactDorian");
			Contact contact2 = (Contact) context.getBean("contactGuerline");
			daoContact.addContact(contact1);
			daoContact.addContact(contact2);
			request.setAttribute("successMessage", "Two contacts have been created.");
			return pMapping.findForward("success");
		}
		return null;
	}
}
